package com.corner.presenter.imp;

import com.amap.api.location.AMapLocation;
import com.corner.model.cornerservice.domain.GeoPoint;
import com.corner.model.cornerservice.domain.Location;
import com.corner.model.cornerservice.helper.LocationHelper;
import com.corner.presenter.ILocationListPresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.view.ILocationListView;
import com.corner.utils.DateFormatUtil;
import com.corner.utils.LocationUtil;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by Administrator on 2015/7/23，09.26.
 * 描述：
 */
public class LocationListPresenter extends BasePresenter<ILocationListView> implements ILocationListPresenter {

    public static final int MAX_TIME_SPAN = 20000;
    private List<Location> locations;
    private boolean isLoading;
    private long lastRefreshTime;
    private int skip;

    @Override
    public void refressh() {
        if (isLoading) {
            return;
        }
        if (DateFormatUtil.getCurrentTimeInMills() - lastRefreshTime < MAX_TIME_SPAN) {
            view.refreshComplete();
            return;
        }
        isLoading = true;
        lastRefreshTime = DateFormatUtil.getCurrentTimeInMills();
        LocationUtil.getInstance().startLocation();
        LocationUtil.getInstance().getLocation(new LocationUtil.OnLocationGetListener() {
            @Override
            public void onLocationGet(AMapLocation location) {
                GeoPoint geoPoint = new GeoPoint();
                geoPoint.setLatitude(location.getLatitude());
                geoPoint.setLongtitude(location.getLongitude());
                LocationHelper.getLocations(geoPoint, 10, 0)
                        .observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Location>>() {

                    @Override
                    public void call(List<Location> locations) {
                        if (view != null) {
                            LocationListPresenter.this.locations = locations;
                            skip = locations.size();
                            if (locations.size() < 10) {
                                view.setLoadMoreEnable(false);
                            } else {
                                view.setLoadMoreEnable(true);
                            }
                            view.showRefreshLocationList(locations);
                            view.refreshComplete();
                        }
                        isLoading = false;

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (view != null) {
                            view.refreshComplete();
                        }
                        isLoading = false;
                    }
                });
            }
        });
    }

    @Override
    public void loadMore() {
        if (isLoading || locations == null) {
            return;
        }
        isLoading = true;
        LocationUtil.getInstance().startLocation();
        LocationUtil.getInstance().getLocation(new LocationUtil.OnLocationGetListener() {
            @Override
            public void onLocationGet(final AMapLocation location) {
                GeoPoint geoPoint = new GeoPoint();
                geoPoint.setLatitude(location.getLatitude());
                geoPoint.setLongtitude(location.getLongitude());
                LocationHelper.getLocations(geoPoint, 10, skip)
                        .observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Location>>() {
                    @Override
                    public void call(List<Location> locations) {
                        if (view != null) {
                            if (locations.size() < 10) {
                                view.setLoadMoreEnable(false);
                            } else {
                                view.setLoadMoreEnable(true);
                            }
                            skip += locations.size();
                            locations.addAll(0, LocationListPresenter.this.locations);
                            LocationListPresenter.this.locations = locations;
                            view.showLoadMoreLocationList(locations);
                            view.loadMoreComplete();
                        }
                        isLoading = false;

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (view != null) {
                            view.loadMoreComplete();
                        }
                        isLoading = false;

                    }
                });
            }
        });
    }
}
